/*
 * @Author: szx
 * @Date: 2021-12-24 14:21:21
 * @LastEditTime: 2021-12-24 14:29:36
 * @Description:
 * @FilePath: \leetcode\600-699\654\654.js
 */
function TreeNode(val, left, right) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
}
/**
 * @param {number[]} nums
 * @return {TreeNode}
 */
var constructMaximumBinaryTree = function (nums) {
    if (nums.length == 0) return null;
    let arr = getMaxIndex(nums);
    let max = arr[0],
        index = arr[1];
    const root = new TreeNode(max);
    root.left = constructMaximumBinaryTree(nums.slice(0, index));
    root.right = constructMaximumBinaryTree(nums.slice(index + 1));
    return root;
};
const getMaxIndex = (arr) => {
    let max = arr[0];
    //声明了个变量 保存下标值
    let index = 0;
    for (let i = 0; i < arr.length; i++) {
        if (max < arr[i]) {
            max = arr[i];
            index = i;
        }
    }
    return [max, index];
};
constructMaximumBinaryTree([3, 2, 1, 6, 0, 5]);
